home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / BSP Tree 1.2 / Sources / Graphics / source / quaternion_3d.cp < prev    next >
Encoding:
Text File  |  1995-03-25  |  3.2 KB  |  57 lines  |  [TEXT/MMCC]

  1. //------------------------------------------------------------------------------
  2. //    File:                    quaternion.cp
  3. //    Date:                    9/18/94
  4. //    Author:                Bretton Wade
  5. //
  6. //    Description:    this file contains the methods for a quaternion
  7. //
  8. //------------------------------------------------------------------------------
  9.  
  10. #include "quaternion_3d.h"
  11.  
  12. //------------------------------------------------------------------------------
  13. //    Transformation matrix_3d from a unit quaternion 
  14. //------------------------------------------------------------------------------
  15. matrix_3d    QuaternionMatrix (real x, real y, real z, real w)                                            //    build a transformation matrix_3d from a unit quaternion
  16. {                                                                                                                                                                //    begin
  17.     matrix_3d    m;                                                                                                                                    //    new matrix_3d
  18.     m (0, 0) = w*w + x*x - y*y - z*z;                                                                                            //    assign the first column
  19.     m (0, 1) = R(2.0) * (x*y + w*z);
  20.     m (0, 2) = R(2.0) * (x*z - w*y);
  21.     m (0, 3) = R(0.0);
  22.     m (1, 0) = R(2.0) * (x*y - w*z);                                                                                            //    assign the second column
  23.     m (1, 1) = w*w - x*x + y*y - z*z;
  24.     m (1, 2) = R(2.0) * (y*z + w*x);
  25.     m (1, 3) = R(0.0);
  26.     m (2, 0) = R(2.0) * (x*z + w*y);                                                                                            //    assign the third column
  27.     m (2, 1) = R(2.0) * (y*z - w*x);
  28.     m (2, 2) = w*w - x*x - y*y + z*z;
  29.     m (2, 3) = R(0.0);
  30.     m (3, 0) = R(0.0);                                                                                                                        //    assign the fourth column
  31.     m (3, 1) = R(0.0);
  32.     m (3, 2) = R(0.0);
  33.     m (3, 3) = w*w + x*x + y*y + z*z;
  34.     return m;                                                                                                                                            //    return the matrix_3d
  35. }                                                                                                                                                                //    end
  36.  
  37. //------------------------------------------------------------------------------
  38. //    make a quaternion from two vectors, and return the resulting matrix_3d
  39. //------------------------------------------------------------------------------
  40. matrix_3d    Quaternion (const vector_3d &begin, const vector_3d &end)                            //    map a quaternion to a transformation matrix_3d
  41. {                                                                                                                                                                //    begin
  42.     real        w = begin | end;                                                                                                            //    the quaternion values, w is cosine (theta / 2), where theta is rotation angle
  43.     vector_3d    perp = begin ^ end;                                                                                                    //    compute the vector_3d perpendicular to the begin and end vectors
  44.     real    norm = perp.Norm ();                                                                                                        //    compute the length of the perpendicular vector_3d
  45.     if (norm < EPSILON)                                                                                                                        //    if the length of the perpendicular vector_3d is 0
  46.         return IDENTITY_MATRIX;                                                                                                            //    the 'begin' and 'end' vectors coincide, so return an identity transformation
  47. //#define    HALF_SPEED_QUATERNION
  48. #ifdef    HALF_SPEED_QUATERNION
  49.     real theta = ACOS (w) / R(2.0);                                                                                                //    compute the half angle of the rotation between 'begin' and 'end'
  50.     w = COS (theta);                                                                                                                            //    set the w coefficient to the cosine of the half angle
  51.     perp = (perp / norm) * SIN (theta);                                                                                        //    normalize the perpendicular vector_3d, and scale it by the sine of the half angle
  52. #endif
  53.     return QuaternionMatrix (perp[X], perp[Y], perp[Z], w);                                                //    return the quaternion converted to a matrix_3d
  54. }                                                                                                                                                                //    end
  55.  
  56. //------------------------------------------------------------------------------
  57.